<?php
/**
 * Classe : RequiredDocument
 * @version 1.0
 * @author Alexia
 */
class RequiredDocument
{
	// int
	private $IdRequiredDocument;
	// int
	private $IdTypeDocument;
	// int
	private $ConventionType;
	// varchar
	private $NomDocument;


	public function __construct($id = 0) {
		if (empty($id)) {
			$this->IdRequiredDocument =0;
			$this->IdTypeDocument =0;
			$this->ConventionType =0;
			$this->NomDocument ="";
			$this->requeteOk = true;
			$this->errormsg = "";
		} else {
			$sql = SQL::getInstance()->getConnection();
			$query = "SELECT * FROM RequiredDocument WHERE IdRequiredDocument = :primary";
			$c = $sql->prepare($query);
			$c->bindParam(":primary", $id, PDO::PARAM_INT);
			if ($c->execute()) {
				$result = $c->fetch(PDO::FETCH_OBJ);
				if (!empty($result)) {
					$this->IdRequiredDocument = $result->IdRequiredDocument;
					$this->IdTypeDocument = $result->IdTypeDocument;
					$this->ConventionType = $result->ConventionType;
					$this->NomDocument = $result->NomDocument;
					$this->requeteOk = true;
					$this->errormsg = "RequiredDocument trouv&eacute;e";
				} else {
					$this->requeteOk = false;
					$this->errormsg = "Ce RequiredDocument n'existe pas !";
				}
			} else {
				$this->requeteOk = false;
				$this->errormsg = $c->errorInfo();
			}
		}
	}

	public function insert()
	{
		$sql = SQL::getInstance()->getConnection();
		$query ="
			INSERT INTO RequiredDocument
			(
				IdTypeDocument,
				ConventionType,
				NomDocument
			)
			VALUES
			(
				:IdTypeDocument,
				:ConventionType,
				:NomDocument
		)";
		$sql->beginTransaction();
		try {
			$c = $sql->prepare($query);
			$c->bindParam(":IdTypeDocument", $this->IdTypeDocument, PDO::PARAM_INT);
			$c->bindParam(":ConventionType", $this->ConventionType, PDO::PARAM_INT);
			$c->bindParam(":NomDocument", $this->NomDocument, PDO::PARAM_STR , 255);
			if ($c->execute()) {
				$this->IdRequiredDocument = $sql->lastInsertId();
				$sql->commit();
				$c->closeCursor();
				$this->requeteOk = true;
				$this->errormsg = "Insertion reussie !";
			} else {
				$this->requeteOk = false;
				$this->errormsg = $c->errorInfo();
				$sql->rollback();
				$c->closeCursor();
				$this->IdRequiredDocument = 0;
			}
		} catch (PDOException $Exception) {
			$this->requeteOk = false;
			$this->errormsg = $c->errorInfo();
			$sql->rollback();
			$c->closeCursor();
			$this->IdRequiredDocument = 0;
		}
	}

	public function update()
	{
		$sql = SQL::getInstance()->getConnection();
		$query =
			"UPDATE RequiredDocument SET
				IdTypeDocument = :IdTypeDocument,
				ConventionType = :ConventionType,
				NomDocument = :NomDocument
			WHERE
				IdRequiredDocument = :IdRequiredDocument
		";
		$sql->beginTransaction();
		try {
			$c = $sql->prepare($query);

			$c->bindParam(":IdRequiredDocument", $this->IdRequiredDocument, PDO::PARAM_INT);
			$c->bindParam(":IdTypeDocument", $this->IdTypeDocument, PDO::PARAM_INT);
			$c->bindParam(":ConventionType", $this->ConventionType, PDO::PARAM_INT);
			$c->bindParam(":NomDocument", $this->NomDocument, PDO::PARAM_STR , 255);

			if ($c->execute()) {
				$sql->commit();
				$c->closeCursor();
				$this->requeteOk = true;
				$this->errormsg = "Modification reussie!";
			} else {
				$this->requeteOk = false;
				$this->errormsg = $c->errorInfo();
				$sql->rollback();
				$c->closeCursor();
			}
		} catch (PDOException $Exception) {
			$this->requeteOk = false;
			$this->errormsg = $c->errorInfo();
			$sql->rollback();
			$c->closeCursor();
		}
		return $this->requeteOk;
	}

	public function delete()
	{
		$sql = SQL::getInstance()->getConnection();
		$query = "DELETE FROM RequiredDocument WHERE IdRequiredDocument = :primary";
		$c = $sql->prepare($query);
		$c->bindParam(":primary", $this->IdRequiredDocument, PDO::PARAM_INT);
		$c->execute();
	}


	public function getIdRequiredDocument()
	{
		return $this->IdRequiredDocument;
	}

	public function getIdTypeDocument()
	{
		return $this->IdTypeDocument;
	}

	public function getConventionType()
	{
		return $this->ConventionType;
	}

	public function getNomDocument()
	{
		return $this->NomDocument;
	}

	public function setIdRequiredDocument($IdRequiredDocument)
	{
		$errTab = array("boolOk" => true, "errmsg" => "");
		if (isset($IdRequiredDocument) && !empty($IdRequiredDocument))
		{
			$this->IdRequiredDocument = $IdRequiredDocument;
		}
		else
		{
			$this->IdRequiredDocument = 0;
			$this->boolOK = false;
			$this->errormsg = "Le champ IdRequiredDocument ne peut etre vide";
		}
		return $errTab;
	}

	public function setIdTypeDocument($IdTypeDocument)
	{
		$errTab = array("boolOk" => true, "errmsg" => "");
		if (isset($IdTypeDocument) && !empty($IdTypeDocument))
		{
			$this->IdTypeDocument = $IdTypeDocument;
		}
		else
		{
			$this->IdTypeDocument = 0;
			$this->boolOK = false;
			$this->errormsg = "Le champ IdTypeDocument ne peut etre vide";
		}
		return $errTab;
	}

	public function setConventionType($ConventionType)
	{
		$errTab = array("boolOk" => true, "errmsg" => "");
		if (isset($ConventionType) && !empty($ConventionType))
		{
			$this->ConventionType = $ConventionType;
		}
		else
		{
			$this->ConventionType = 0;
			$this->boolOK = false;
			$this->errormsg = "Le champ ConventionType ne peut etre vide";
		}
		return $errTab;
	}

	public function setNomDocument($NomDocument)
	{
		$errTab = array("boolOk" => true, "errmsg" => "");
		if (isset($NomDocument) && !empty($NomDocument))
		{
			$this->NomDocument = $NomDocument;
		}
		else
		{
			if (!isset($NomDocument))
			{
				$errTab["errmsg"] = "Le NomDocument ne peut &ecirc;tre vide !";
			}
			elseif (empty($NomDocument))
			{
				$errTab["errmsg"] = "Le NomDocument ne peut &ecirc;tre vide !";
			}
			elseif(strlen($NomDocument) > 255)
			{
				$errTab["errmsg"] = "Le NomDocument doit &ecirc;tre compris entre 1 et 255 caract&egrave;res !";
			}
			$this->NomDocument = "";
			$this->boolOK = false;
		}
		return $errTab;
	}

     /**
     * Renvoi la liste complète des documents obligatoires (attestation sécurité vitale...)
     * @return listRequiredDocuments
     */
	static public function getList()
	{
		$sql = SQL::getInstance()->getConnection();
		$query = "SELECT * FROM RequiredDocument ";
		$c = $sql->query($query);
		$result = $c->fetchAll(PDO::FETCH_OBJ);
		if(!empty($result))
		{
			return $result;
		}
		return array();
	}

    /**
     * Retourne la liste des RequiredDocument par stage
     * @param int $idStage
     * @return <type> 
     */
    public static function getListRequiredDocByStage($idStage){
        //Recuperation des variables globales
        $sql = SQL::getInstance()->getConnection();
        $query = '  SELECT D.*, R.NomRequiredDocument, R.IdRequiredDocument
                    FROM RequiredDocument R
                    INNER JOIN Document D ON D.IdRequiredDocument = R.IdRequiredDocument
                    WHERE D.IdStage = :stage
                    AND D.IdRequiredDocument <> 0
                    ORDER BY R.IdRequiredDocument';
        $c = $sql->prepare($query);
        $c->bindParam(':stage', $idStage, PDO::PARAM_INT);
        if($c->execute()){
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }
}
?>